package com.chenyaohua.file;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.LinkedList;
import java.util.List;

/**
 * 获取文件夹信息
 */
public class FileTreeListVisitor extends SimpleFileVisitor<Path> {
    /**
     * 全部目录
     */
    private List<File> dirs = new LinkedList<>();
    /**
     * 全部文件
     */
    private List<File> files = new LinkedList<>();

    @Override
    public FileVisitResult preVisitDirectory (Path dir, BasicFileAttributes attrs) throws IOException {
        dirs.add(dir.toFile());
        return super.preVisitDirectory(dir, attrs);
    }

    @Override
    public FileVisitResult visitFile (Path file, BasicFileAttributes attrs) throws IOException {
        files.add(file.toFile());
        return super.visitFile(file, attrs);
    }

    @Override
    public FileVisitResult visitFileFailed (Path file, IOException exc) throws IOException {
        return super.visitFileFailed(file, exc);
    }

    public List<File> getDirs () {
        return dirs;
    }

    public List<File> getFiles () {
        return files;
    }
}
